package com.feib.stms.batch.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer;
import org.springframework.stereotype.Repository;

import com.feib.stms.batch.dao.rowmapper.CodeTableRowMapper;
import com.feib.stms.batch.domain.CodeTable;



@Repository("codeTableJdbcDao")
public class CodeTableJdbcDao extends BaseJdbcDao {


	private OracleSequenceMaxValueIncrementer sequence;

	private SimpleJdbcInsert codeTableJdbcInsert;


	@Override
	protected void initDao() throws Exception {
		super.initDao();
		this.sequence = new  OracleSequenceMaxValueIncrementer(this.getDataSource(), "CODE_TABLE_PK_SEQ");

		this.codeTableJdbcInsert = new  SimpleJdbcInsert(this.getDataSource());
		this.codeTableJdbcInsert.withTableName("CODE_TABLE")
		.usingColumns("CODE_TABLE_PK", "CODE_KIND", "CODE_VALUE" ,"CODE_NAME" ,"SORT_INDEX"  				 
				,"CREATED_BY" ,"CREATED_DATE" ,"UPDATED_BY" ,"UPDATED_DATE" );
	}




	public CodeTable getCodeTable(String codeKind, String codeValue)

	{
		StringBuffer sql = new StringBuffer();
		Map<String, Object> parameters = new HashMap<String, Object>();

		sql.append("SELECT C.*");
		sql.append(" FROM  CODE_TABLE   C ");
		sql.append(" WHERE C.CODE_KIND =:CODE_KIND AND C.CODE_VALUE =:CODE_VALUE ");

		parameters.put("CODE_KIND", codeKind);
		parameters.put("CODE_VALUE", codeValue);

		logger.debug("SQL: {}", sql.toString());
		logger.debug("Args: {}", parameters);

		try{
			return DataAccessUtils.singleResult(this.getSimpleJdbcTemplate().query(sql.toString(), new CodeTableRowMapper(), parameters));
		}catch (EmptyResultDataAccessException e) {
			return null;
		}

	}


	public int insert(CodeTable codeTable) {

		Map<String, Object> parameters = new HashMap<String, Object>();
		Long codeTablePk = new Long(this.sequence.nextLongValue());

		parameters.put("CODE_TABLE_PK",  codeTablePk);
		parameters.put("CODE_KIND",  codeTable.getCodeKind());
		parameters.put("CODE_VALUE",  codeTable.getCodeValue());
		parameters.put("CODE_NAME",  codeTable.getCodeName());
		parameters.put("SORT_INDEX",  codeTable.getSortIndex());
		parameters.put("CREATED_BY",  codeTable.getCreatedBy());
		parameters.put("CREATED_DATE", new Date());

		
		logger.debug("Args: {}", parameters);

		int insertCounts = this.codeTableJdbcInsert.execute(parameters);

		if (insertCounts > 0)
			codeTable.setCodeTablePk(codeTablePk);
		return insertCounts;

	}

	public int update(CodeTable codeTable) {

		Map<String, Object> parameters = new HashMap<String, Object>();

		StringBuffer sql = new StringBuffer();
		sql.append(" UPDATE CODE_TABLE " );
		sql.append(" SET CODE_NAME = :CODE_NAME ");
		sql.append(" ,UPDATED_DATE = :UPDATED_DATE ");
		sql.append(" ,UPDATED_BY = :UPDATED_BY ");
		sql.append(" WHERE CODE_TABLE_PK = :CODE_TABLE_PK ");

		parameters.put("CODE_NAME", codeTable.getCodeName());
		parameters.put("UPDATED_DATE",  codeTable.getUpdatedDate());
		parameters.put("UPDATED_BY",  codeTable.getUpdatedBy());
		parameters.put("CODE_TABLE_PK",  codeTable.getCodeTablePk());

		logger.debug("SQL: {}", sql.toString());
		logger.debug("Args: {}", parameters);

		return this.getSimpleJdbcTemplate().update(sql.toString(), parameters);

	}

}
